package ygstudio.xue.de.activity;

import ygstudio.xue.de.R;
import ygstudio.xue.de.RuntimeUtils;
import ygstudio.xue.de.db.DB;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Update extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update);

		Button importButton = (Button) findViewById(R.id.importButton);
		importButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int count = DB.getInstance().importXml(Update.this);
				if (count > 0) {
					Toast toast = Toast.makeText(Update.this, count
							+ " importiert!", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		});

		Button exportButton = (Button) findViewById(R.id.exportButton);
		exportButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DB.getInstance().exportXml(Update.this,
						RuntimeUtils.getInstance().getSdDefaultXml());
			}
		});

		Button clear = (Button) findViewById(R.id.clearButton);
		clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(
						Update.this);
				dialog.setTitle("Clear DB?");
				dialog.setNegativeButton("NO",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						});

				dialog.setPositiveButton("OK",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								 DB.getInstance().clearDB();
							}
						});
				dialog.show();
			}
		});

	}

}
